// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/broadcast/api/BroadcastTaskApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/broadcast/model/QueryTaskListReq'
import OneClass from '@api/broadcast/model/TerminalBaseReq'
import AddClass from '@api/broadcast/model/InsertTaskReq'
import EditClass from '@api/broadcast/model/BroadcastTaskSetVolReq'
import SubmitClass from '@api/broadcast/model/TaskControlReq'
import MusicDTO from '@api/broadcast/model/MusicDto'
import TerminalDTO from '@api/broadcast/model/AddressBookDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const MUSIC = [MusicDTO, 'musicList', 1]
const ERMINAL = [TerminalDTO, 'terminalList', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryTaskList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id: [id] })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryTaskDetail', DTO)
    return resp.content || {}
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, MUSIC, ERMINAL)
    await CORE.grpc(TIP, CLIENT, 'insertTask', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one)
    await CORE.grpc(TIP, CLIENT, 'broadcastTaskSetVol', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async submit(one, TIP = '提交') {
    const DTO = CORE.grpcDTO(SubmitClass, one)
    await CORE.grpc(TIP, CLIENT, 'taskControl', DTO)
    return CORE.grpcOK(TIP)
  },
}
